package demo2;

import javax.sql.XAConnection;

/**
 * Created with  IntelliJ IDEA .
 * Description:
 * User: lenovo
 * Date: 2025-11-25
 * Time: 19:54
 */

class Money implements Cloneable {
    public double money = 9.9;

    @Override
    protected Object clone()
            throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{

    public int age;
    public String name;

    Money m = new Money();



    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone()
            throws CloneNotSupportedException {
        Person tmp = (Person) super.clone();
        tmp.m = (Money) this.m.clone();
        return tmp;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class Test {

    public static void main(String[] args)
            throws CloneNotSupportedException {
        Person person1 = new Person("zhangsan",55);
        Person person2 = (Person) person1.clone();

        System.out.println(person1+" "+person2);

        System.out.println("修改前："+person1.m.money);
        System.out.println("修改前："+person2.m.money);


        person2.m.money = 99.99;

        System.out.println("修改后："+person1.m.money);
        System.out.println("修改后："+person2.m.money);

    }

}
